package com.dangf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dangf.entity.Review;
import com.dangf.service.ReviewService;

/**
 * 评论管理 控制器
 * @author Hello_海生
 * @date 2014年3月27日
 */
@Controller
@RequestMapping("/review")
public class ReviewController {
	@Autowired
	private ReviewService reviewService;
	
	//发布评论
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> insert(String json){
		Review review = JSON.parseObject(json, Review.class);
		
		System.out.println("JS:---------------"+JSON.toJSONString(review));
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		if(review.getUserId()!=null && !review.getContent().equals("")){
			long id = (Long) reviewService.save(review);
			if(id!=0){
				Review r = reviewService.findById(id);
				map.put("message", "恭喜你，评论发布成功！");
				map.put("static","success");
				map.put("review",r);
				}else{
					map.put("message", "系统错误，评论失败！");
					map.put("static","error");
				}
		}else{
			map.put("message", "发布失败，请输入评论内容或发布者Id。");
			map.put("static","error");
		}
		return map;
	}
	
	//查询评论信息
	@RequestMapping("/show")
	@ResponseBody
	public Map<String, Object> show(long id){
		Map<String, Object> map = new HashMap<String, Object>();
		if(id!=0){
			Review r= reviewService.findById(id);
			if(r!=null){
				map.put("message", "信息查询成功！");
				map.put("static","success");
				map.put("review",r);
			}else{
				map.put("message", "查询失败，此此 评论  不存在。");
				map.put("static","error");
			}
		}else{
			map.put("message", "查询失败，请输入评论Id");
			map.put("static","error");
		}
		return map;
	}
	
	//查询所有 文章里 所有信息
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(long id,int start,int count){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Review> r= reviewService.findReviewAll(id, start, count);
		if(r!=null){
			map.put("message", "信息查询成功！");
			map.put("static","success");
			map.put("reviews",r);
		}else{
			map.put("message", "查询失败，不知出何错误！");
			map.put("static","error");
		}
		return map;
	}
	
}
